package com.sgu6.sgu6_machine.ui.fragment.set;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgu6.middleware.enums.MachineCmdEnum;
import com.sgu6.middleware.util.HexUtil;
import com.sgu6.serialport.manager.SerialPortManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.ui.views.AmountView;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 电机设置
 */
public class AccuracyF extends BaseF {

    @BindView(R.id.atDivisionAdjust)
    AmountView atDivisionAdjust;
    @BindView(R.id.atSetAccuracy)
    AmountView atSetAccuracy;
    @BindView(R.id.atSteps)
    AmountView atSteps;
    private int curDivision = 1; //当前分度
    public Timer accuracyTimer = new Timer();

    public static AccuracyF newInstance() {
        return new AccuracyF();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_accuracy, container, false);
        unbinder = ButterKnife.bind(this, v);

        initUI();

        return v;
    }

    public void initUI() {
        atDivisionAdjust.setDefaultValue(MachineContainer.machineConfigDto.millAccuracyDivisions);
        atDivisionAdjust.setAmountMaxValue(10);

        atSetAccuracy.setDefaultValue(MachineContainer.machineConfigDto.accuracyDivisions);
        atSetAccuracy.setAmountMaxValue(10);

        atSteps.setDefaultValue(MachineContainer.machineConfigDto.accuracySteps);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.btnAccuracyAdjust, R.id.btnAccuracySet, R.id.btnAccuracyStepSet})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnAccuracyAdjust:
                String cmdAdjust = String.format(MachineCmdEnum.精度步数调节.code(), atDivisionAdjust.getValue());
                StringBuilder sb = new StringBuilder(cmdAdjust);
                //5AA509820043000100070000
                sb.replace(15, 16, String.valueOf(MachineContainer.machineConfigDto.mode));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(sb.toString()));
                MachineContainer.machineConfigDto.millAccuracyDivisions = atDivisionAdjust.getValue();
                updateConfig();
                break;
            case R.id.btnAccuracySet://设置精度
                String cmdSet = String.format(MachineCmdEnum.精度步数设置.code(), (0x15 + atSetAccuracy.getValue()), atSteps.getValue() * atSetAccuracy.getValue());
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(cmdSet));
                MachineContainer.machineConfigDto.accuracyDivisions = atSetAccuracy.getValue();
                MachineContainer.machineConfigDto.accuracySteps = atSteps.getValue();
                updateConfig();

                break;
            case R.id.btnAccuracyStepSet://一键设置10个精度
                accuracyTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        String command;
                        if (curDivision < 11) {
                            command = String.format(MachineCmdEnum.精度步数设置.code(), (0x15 + curDivision), atSteps.getValue() * curDivision);
                            SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(command));
                            curDivision++;
                        } else {
                            curDivision = 1;
                            cancel();
                        }
                    }
                }, 500, 1000);
                MachineContainer.machineConfigDto.accuracyDivisions = atSetAccuracy.getValue();
                MachineContainer.machineConfigDto.accuracySteps = atSteps.getValue();
                updateConfig();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        accuracyTimer = null;
    }
}
